<?php

namespace App\Index\Controller;

use SinglePHP\BaseController;
use SinglePHP\Db;

class School extends BaseController
{

    public function index()
    {
        ini_set('max_execution_time', '0');

        $datas = Db::query("select * from waiguoyu_teacher")->fetchAll();
//        dump($datas);
//        die;
        foreach ($datas as $data) {

            $depms = explode(';', $data['depmt']);
            foreach ($depms as $depm) {
                $last_id = null;
                $user_depms = explode('/', $depm);
                $this->updatedepmts(0, $user_depms, $last_id);
//                dump([ '$last_id 最终：' => $last_id ]);
                $res = Db::query("select * from waiguoyu_teahcer_depmt where depmt_id = {$last_id} and teacher_id = {$data['teacher_id']}")->fetch();
//                dump([ '判断是否有老师对应的部门', $res, DB::last() ]);
                if (!$res && $last_id != null) {
                    $fast_depmt_id = $this->getOnelevelId($last_id);
                    $test = Db::query("select * from waiguoyu_teahcer_depmt where teacher_id = {$data['teacher_id']} and fast_depmt_id = $fast_depmt_id")->fetch();
                    if ($test) {
                        Db::insert('waiguoyu_teahcer_depmt', [ 'depmt_id' => $last_id, 'teacher_id' => $data['teacher_id'] ]);
                    } else {
                        Db::insert('waiguoyu_teahcer_depmt', [ 'depmt_id' => $last_id, 'teacher_id' => $data['teacher_id'], 'fast_depmt_id' => $fast_depmt_id ]);
                    }

//                    dump([ '不存在则创建', DB::last() ]);
                }
            }
        }
    }

    private function updatedepmts($parent_id = 0, $depmts = [], &$last_id = null, $level = 0)
    {
//        dump([ '$last_id' => $last_id ]);
        if (count($depmts) > 0) {
            $depmt = array_shift($depmts);
            $parent_res = DB::query("select * from waiguoyu_depmt  where depmt_pid = {$parent_id} and  depmt_name = '{$depmt}'")->fetch();
            // dump([ '$depmts' => $depmts, '$parent_res' => $parent_res, Db::last() ]);
            $level++; //部门每一层就相应加一
            if (!$parent_res) {
                Db::insert('waiguoyu_depmt', [ 'depmt_pid' => $parent_id, 'depmt_name' => $depmt, 'level' => $level ]);
                $last_id = $id = Db::id();
                $this->updatedepmts($id, $depmts, $last_id, $level);
            } else {
                $parent_res = DB::query("select * from waiguoyu_depmt  where depmt_pid = {$parent_id} and  depmt_name = '{$depmt}'")->fetch();
                $last_id = $parent_res['depmt_id'];
                $this->updatedepmts($parent_res['depmt_id'], $depmts, $last_id, $level);
            }

        }
    }


    private function getOnelevelId($chil_id = 0)
    {
        $depmts_ = Db::query('select depmt_id,depmt_pid from waiguoyu_depmt where depmt_pid >1')->fetchAll();

        $depmts = [];
        foreach ($depmts_ as $depmt) {
            $depmts[$depmt['depmt_id']] = (int)$depmt['depmt_pid'];
        }
        while ($depmts[$chil_id]) {
            $chil_id = $depmts[$chil_id];
        }
        return $chil_id;
    }
}